#!/bin/bash
# File Name: 00-header
# Author: moshan
# mail: mo_shan@yeah.net
# Created Time: 2019-05-09 09:23:26
# Function: motd
#########################################################################
alert=(10 30)

color1="\033[31m"
color2="\033[32m"
color3="\033[33m"
color4="\033[34m"
color5="\033[35m"
color6="\033[36m"
color7="\033[37m"
color_end="\033[0m"
mysql_path="/data/mysql/base/bin/mysql"
mysql_user="moshan"
mysql_passwd="123456"
mysql_host="127.0.0.1"
mysql_port="3306"
mysql_comm="${mysql_path} -u${mysql_user} -p${mysql_passwd} -h${mysql_host} -P${mysql_port}"

date="$(date "+%F %T")"
head="System information as of: ${date}"
 
kernel="$(uname -r)"
hostname="${HOSTNAME}"
 
#Cpu load
load1="$(cat /proc/loadavg | awk '{print $1}')"
load5="$(cat /proc/loadavg | awk '{print $2}')"
load15="$(cat /proc/loadavg | awk '{print $3}')"
 
#System uptime
uptime="$(cat /proc/uptime | cut -f1 -d.)"
upDays="$((uptime/60/60/24))"
upHours="$((uptime/60/60%24))"
upMins="$((uptime/60%60))"
upSecs="$((uptime%60))"
up_lastime="$(date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S")"
 
#Memory Usage
mem_usage="$(free -m | awk '/Mem:/{total=$2;used=$3;free=$4;buff=$6;avail=$7} END {print "total:"total"MB, used:"used/total*100"%, free:"free/total*100"%, buff/cache:"buff"MB, available:"avail"MB"}')"
swap_usage="$(free -m | awk '/Swap/{printf "total:"$2"MB, used:%.2f%",(1-$4/$2)}')"
 
#Processes
processes="$(top -n 1|grep -i tasks)"
 
#User
users="$(users | wc -w)"
USER="$(whoami)"
 
#System fs usage
Filesystem="$(df -h | awk '/^\/dev/{print $6}')"
 
#Interfaces
INTERFACES="$(ip -4 ad | grep 'state ' | awk -F":" '!/^[0-9]*: ?lo/ {print $2}')"
 
echo
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "${head}"
echo "----------------------------------------------"
printf "Kernel Version:\t%s\n" ${kernel}
printf "HostName      :\t%s\n" ${hostname}
printf "System Load   :\t%s %s %s\n" ${load1}, ${load5}, ${load15}
printf "System Uptime :\t%s "days" %s "hours" %s "min" %s "sec"\n" ${upDays} ${upHours} ${upMins} ${upSecs}
printf "Memory Usage  :\t%s\t\n" "${mem_usage}"
printf "Swap Usage    :\t%s\n" "${swap_usage}"
printf "Login Users   :\t%s\t\t\tWhoami:\t\t%s\n" ${users} ${USER}
printf "Processes     :\t%s\n" "${processes}"
printf "\n"
printf "\n"
printf "Interface\tMAC Address\t\tIP Address\n"
for i in ${INTERFACES}
do
    MAC=$(ip ad show dev ${i} | grep "link/ether" | awk '{print $2}')
    IP=$(ip ad show dev ${i} | awk '/inet / {print $2}')
    printf ${i}"\t\t"$MAC"\t${IP}\n"
done
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo



col_array=(50 10 10 10 10 48)

function f_format_print_info()
{
    show_str="${1}"
    if [ "$(grep -cE "^space_str|space_str$|space_strs$" <<< "${show_str}")x" == "1x" ]
    then
        show_str_len=$((${#show_str}-9))
    else
        show_str_len=${#show_str}
    fi
    max_len="${2}"
    tmp_len=$((${max_len}-${show_str_len}))
    for ((var=0;var<${tmp_len};var++))
    do
        if [ "${1}x" == "-x" ]
        then
            show_str="${show_str}-"
        else
            show_str="${show_str}space_str"
        fi
    done
}

function f_enter_str()
{
    var_pos=0
    str_type=(0 0 0)
    clo=(0 0 0)
    for var in ${@}
    do
        col_len="${col_array[${var_pos}]}"
        eval str_type[${var_pos}]="${var}"
        f_format_print_info "${str_type[${var_pos}]}" "${col_len}"
        eval clo[${var_pos}]="${show_str}"
        var_pos=$((${var_pos}+1))
		i=$((${i}+1))
    done
    show_info=""
    for ((i=0;i<${#clo[@]};i++))
    do
        if [ "${i}x" == "0x" ]
        then
            if [ "${str_type[0]}x" == "-x" ]
            then
                show_info="${show_info}${color5}+${clo[${i}]}-+-"
            else
                show_info="${color5} ${color7}${clo[${i}]}${color5} ${color_end}"
            fi
        elif [ "${i}x" == "$((${#clo[@]}-1))x" ]
        then
            if [ "${str_type[0]}x" == "-x" ]
            then
                show_info="${show_info}${clo[${i}]}-+${color_end}"
            else
                show_info="${show_info}${color7} ${clo[${i}]}${color5} ${color_end}"
            fi
        else
            if [ "${str_type[0]}x" == "-x" ]
            then
                show_info="${show_info}${clo[${i}]}-+-"
			elif [ "$(awk -F% -v v1=${alert[1]} '{print $1-v1}' <<< "${str_type[4]}"|grep -c "^-")x" == "0x" -a "${i}x" == "$((${#clo[@]}-2))x" ]
			then
                show_info="${show_info}${color1} ${clo[${i}]} ${color5} ${color_end}"
			elif [ "$(awk -F% -v v1=${alert[0]} '{print $1-v1}' <<< "${str_type[4]}"|grep -c "^-")x" == "0x" -a "${i}x" == "$((${#clo[@]}-2))x" ]
			then
                show_info="${show_info}${color3} ${clo[${i}]} ${color5} ${color_end}"
			elif [ "$(awk -F% -v v1=${alert[0]} '{print $1-v1}' <<< "${str_type[4]}"|grep -c "^-")x" == "1x" -a "${i}x" == "$((${#clo[@]}-2))x" ]
			then
				[ "${str_type[4]}x" == "Use%x" ] && color2="\033[34m" || color2="\033[32m"
                show_info="${show_info}${color2} ${clo[${i}]} ${color5} ${color_end}"
            else
                show_info="${show_info}${color7} ${clo[${i}]}\033[0m ${color5} ${color_end}"
            fi
        fi
    done
    echo -e "*${show_info}*"|sed 's/space_str/ /g'
}

#MySQL status START
mysql_version=$(${mysql_comm} -NBe "select version()" 2>/dev/null)
if [ "${mysql_version}x" != "x" ]
then
	color2="\033[1;32m"
	mysql_status="Running"
	mysql_version="$(awk -F-log '{print $1}' <<< "${mysql_version}")"
	size="$(${mysql_comm} -NBe "select if(size > 1024,concat(size/1024,' GB'),concat(size,' MB')) from (select round(sum(data_length+index_length)/1024/1024,2) size from information_schema.tables)a;" 2>/dev/null)"
	[ "${#size}x" != "8x" ] && size="0${size}"
	uptime="$(${mysql_comm} -NBe "show global status like 'uptime'" 2>/dev/null|awk '{print $2}'|awk '{if($1 < 60){d="00";h="00";m="00";s=$1%60;if(s < 10){s="0"s;}$1=d" days "h" hours "m" mins "s" secs ";print $0;next;}else if($1 >= 60 && $1 < 3600){d="00";h="00";m=int($1/60);if (m < 10){m="0"m;}s=$1%60;if ( s < 10 ){s="0"s;}$1=d" days "h" hours "m" mins "s" secs ";print $0;next;}else if($1 >= 3600 && $1 < 86400){d="00";h=int($1/3600);if (h < 10){h="0"h;}m=int($1%3600/60);if (m < 10){m="0"m;}s=$1%60;if ( s < 10 ){s="0"s;}$1=d" days "h" hours "m" mins "s" secs ";print $0;next;}else if($1 > 86400){d=int($1/86400);if (d < 10){d="0"d;}h=int($1%86400/3600);if (h < 10){h="0"h;}m=int($1%3600/60);if (m < 10){m="0"m;}s=$1%60;if ( s < 10 ){s="0"s;}$1=d" days "h" hours "m" mins "s" secs ";print $0;}}')"
else
	color2="\033[31m"
	mysql_status="ERROR  "
	mysql_version="ERROR "
	uptime="ERROR                            "
	size="ERROR   "
fi
echo "**************************************************************************************************************************************************************"
echo "**************************************************************** MySQL Information ***************************************************************************"
echo -e "* Status  : ${color2}${mysql_status}${color_end}                                                                                                                                          *"
echo -e "* Version : ${color2}${mysql_version}${color_end}                                                                                                                                           *"
echo -e "* Size    : ${color2}${size}${color_end}                                                                                                                                         *"
echo -e "* Uptime  : ${color2}${uptime}${color_end}                                                                                                                *"
color2="\033[32m"

echo "**************************************************************************************************************************************************************"
echo "********************************************************** Strorage Volume Information ***********************************************************************"

#MySQL status END


index=1
df -h|grep -vE "^udev|^tmpfs|^cgmfs"|while read line
do 
    header=(${line})
	if [ "${index}x" == "1x" ]
	then
		color7="\033[34m"
	else
		color7="\033[37m"
	fi
    f_enter_str "space_str${header[0]}" "${header[1]}" "${header[2]}" "${header[3]}" "${header[4]}" "${header[5]}space_str"
	index=$((${index}+1))
done
echo "**************************************************************************************************************************************************************"
